package elements;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class Board extends JFrame {

	private static final long serialVersionUID = 1L;
	public static int boardWidth = 800;
	public static int boardHeight = 600;

	public static void main(String[] args) {
		new Board();
	}

	public Board() {
		this.setSize(boardWidth, boardHeight);
		this.setTitle("Java Asteroids");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		GameDrawingPanel gamePanel = new GameDrawingPanel();

		this.add(gamePanel, BorderLayout.CENTER);

		// argument - core pool size
		ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(
				5);
		executor.scheduleAtFixedRate(new RepaintTheBoard(this), 0L, 20L,
				TimeUnit.MILLISECONDS);

		this.setVisible(true);

	}

}

class RepaintTheBoard implements Runnable {

	Board theBoard;

	public RepaintTheBoard(Board theBoard) {
		this.theBoard = theBoard;
	}

	@Override
	public void run() {
		theBoard.repaint();
	}
}

class GameDrawingPanel extends JComponent {

	private static final long serialVersionUID = 1L;
	public ArrayList<Rock> rocks = new ArrayList<Rock>();
	int[] polyXArray = Rock.sPolyXArray;
	int[] polyYArray = Rock.sPolyYArray;

	int width = Board.boardWidth;
	int height = Board.boardHeight;

	public GameDrawingPanel() {

		for (int i = 0; i < 50; i++) {
			int randomStartXPos = (int) (Math.random() * (width - 40) + 1);
			int randomStartYPos = (int) (Math.random() * (height - 40) + 1);

			rocks.add(new Rock(Rock.getPolyXArray(randomStartXPos), Rock
					.getPolyYArray(randomStartYPos), polyXArray.length,
					randomStartXPos, randomStartYPos));
		}
	}
	
	public void paint(Graphics g){
		Graphics2D graphicSettings = (Graphics2D)g;		
		graphicSettings.setColor(Color.BLACK);
		graphicSettings.fillRect(0, 0, getWidth(), getHeight());
		
		graphicSettings.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		graphicSettings.setPaint(Color.WHITE);
		
		for(Rock rock: rocks){
			rock.move();
			graphicSettings.draw(rock);
		}
	}

	
	
}
